package com.kc.business.service.impl;

import com.kc.business.domain.BizIndustry;
import com.kc.business.mapper.BizIndustryMapper;
import com.kc.business.service.IBizIndustryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @ClassName BizIndustryServiceImpl
 * @Description 行业相关实现类
 * @Author wangdongliang
 * @Date 2021/5/25 9:56
 */
@Service
@Slf4j
public class BizIndustryServiceImpl implements IBizIndustryService {
    @Resource
    private BizIndustryMapper bizIndustryMapper;
    @Override
    public Integer add(BizIndustry bizIndustry) {
        bizIndustry.setCreateTime(LocalDateTime.now());
        bizIndustry.setUpdateTime(LocalDateTime.now());
        return bizIndustryMapper.insert(bizIndustry);
    }

    @Override
    public Integer update(BizIndustry bizIndustry) {
        bizIndustry.setUpdateTime(LocalDateTime.now());
        return bizIndustryMapper.updateByPrimaryKeySelective(bizIndustry);
    }

    @Override
    public List<BizIndustry> getList(BizIndustry bizIndustry) {
        List<BizIndustry> bizIndustries = bizIndustryMapper.getList(bizIndustry);
        List<BizIndustry> bizIndustryList = new ArrayList<>();
        for (BizIndustry industry : bizIndustries){
            industry.setChildred(getChild(bizIndustries,industry.getId()));
            if(industry.getPid().equals(0)){
                bizIndustryList.add(industry);
            }
        }
        return bizIndustryList;
    }

    public List<BizIndustry> getChild(List<BizIndustry> bizIndustryList, Integer id){
        List<BizIndustry> bizIndustries = bizIndustryList.stream().map(item->{return item;}).filter(item->{return item.getPid().equals(id);}).collect(Collectors.toList());
        return bizIndustries;
    }
}
